﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using YT.DB;
using YT.Commons;
using System.Runtime.InteropServices;
using System.Data.SqlClient;

namespace YT_XHProject
{
    public partial class frmInputCode : Form
    {
        bool blnCancel = true;
        public frmInputCode()
        {
            InitializeComponent();
        }

        public bool  ShowForm(ref string strUserName)
        {
            txtUserName.Text = strUserName;
            this.ShowDialog();
            string strHospitalID = txtHospitalID.Text;
            if (!CheckHospitalID ( strHospitalID )) return false;
            if (blnCancel) return false;

            {
                if (strHospitalID.Trim().ToUpper() == "")
                {
                    return false ;
                }
                else
                {
                    DBAccess dbcon = new DBAccess(false, false);
                    string strSQL = "update mHospital set IsCurHospital=1 where HospitalID='" + strHospitalID + "'";
                    dbcon.ExecuteSql(strSQL);
                    dbcon.CloseCon();
                }
            }
            return true;

        }

        private bool CheckHospitalID(string strHospitalID)
        {
            DBAccess dbcon = new DBAccess(false, false);
            string strSQL = string.Empty;
            strSQL = "select * from mHospital where HospitalID='"+ strHospitalID +"'";
            DataRow drRow = dbcon.GetDataFirstRow(strSQL);
            if (drRow == null)
            {
                MessageBox.Show("错误的医院编号，请检查后重新输入。", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
                return false;
            }
            lblName.Text = CvtFunc.ToString(drRow["HospitalName"]);

            strSQL = "select * from mUser where HospitalID='" + strHospitalID + "' and userno='"+ txtUserName.Text +"'";
            drRow = dbcon.GetDataFirstRow(strSQL);
            if (drRow == null)
            {
                MessageBox.Show("错误的用户编号，请检查后重新输入。", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            dbcon.CloseCon();
            blnCancel = false;
            return true;
        }

        private void btnNo_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.Default;
            this.Close();
        }

        private void btnYes_Click(object sender, EventArgs e)
        {
            blnCancel = false;
            if (CheckHospitalID(txtHospitalID.Text)) this.Hide();
        }

        private void txtHospitalID_TextChanged(object sender, EventArgs e)
        {
            DBAccess dbcon = new DBAccess(false, false);
            string strSQL = string.Empty;
            strSQL = "select * from mHospital where HospitalID='" + txtHospitalID.Text  + "'";
            DataRow drRow = dbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                lblName.Text = CvtFunc.ToString(drRow["HospitalName"]);
            }
            else
                lblName.Text = "";
            dbcon.CloseCon();
        }


    }
}
